package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dtos.PageResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.threadlocal.UserThreadLocalUtils;
import com.heima.file.service.MinioService;
import com.heima.model.media.dtos.WmMaterialDto;
import com.heima.wemedia.entity.WmMaterial;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.lang.invoke.LambdaConversionException;
import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 自媒体图文素材信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2022-09-15
 */
@Slf4j
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private MinioService minioService;

    /**
     * 上传素材
     * @param multipartFile
     * @return
     */
    @Override
    public String upload(MultipartFile multipartFile) {
//        用户id
        Integer userId = UserThreadLocalUtils.getUserId();
//        获取文件原始名字
        String originalFilename = multipartFile.getOriginalFilename();
//        获取后缀
        String suffix = StringUtils.substringAfterLast(originalFilename, ".");
        log.info("suffix=={}",suffix);
//        生成新的文件名字
        String newFileName = UUID.randomUUID().toString() + "." +suffix;
//        把图片上传到minio，获取文件路径
        try {
            String url = minioService.uploadImgFile(null, newFileName, multipartFile.getInputStream());
            //        保存到素材表中
            WmMaterial wmMaterial = new WmMaterial();
            wmMaterial.setUrl(url);
            wmMaterial.setType(0);
            wmMaterial.setCreatedTime(new Date());
            wmMaterial.setUserId(userId);
            save(wmMaterial);
            return url;
        } catch (IOException e) {
            log.error("上传图片失败");
            e.printStackTrace();
            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR);
        }

    }

    @Override
    public PageResult<WmMaterial> findByPage(WmMaterialDto dto) {

        Integer userId = UserThreadLocalUtils.getUserId();

        Page<WmMaterial> ipage = new Page<>(dto.getPage(), dto.getSize());

        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.
                eq(WmMaterial::getUserId,userId)
                .eq(dto.getIsCollection() !=0,WmMaterial::getIsCollection,dto.getIsCollection());

        Page<WmMaterial> wmMaterialPage = page(ipage, queryWrapper);
        
        return new PageResult<>(dto.getPage(),
                dto.getSize(),
                wmMaterialPage.getTotal(),
                wmMaterialPage.getRecords());
    }
}
